<?php
/*
Plugin Name: 百度云加速
Plugin URI: https://www.beizigen.com/3499.html
Description: 更新文章自动刷新百度CDN缓存。
Version: 2.1.0
Author: 背字根
Author URI: https://www.beizigen.com/
Text Domain: baidusu
License: GPL v3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
require_once('includes/class-baidusu.php');
require_once('includes/functions.php');
function baidusu_control() {
	if(isset($_POST['action'])) {
		//保存设置
		if('save' == $_POST['action']) {
			$data = array(
				'access_key' => sanitize_text_field($_POST['access_key']),
				'secret_key' => sanitize_text_field($_POST['secret_key']),
			);
			$data = serialize($data);
			update_option('baidusu', $data);
			echo '<div class="notice is-dismissible updated">设置保存成功！</div>';
		}
		//手动刷新
		if('立即刷新' == $_POST['action']) {
			$urlstr = sanitize_textarea_field($_POST['refresh']);
			if($urlstr) {
				$urls = explode("\r\n", $urlstr);
				$baiduSu = new baiduSu;
				$response = $baiduSu->refresh_cache($urls);
				if($response !== true) {
					echo '<div class="notice is-dismissible error">' . $response . '</div>';
				} else {
					echo '<div class="notice is-dismissible updated">刷新CDN缓存成功！</div>';
				}
			} else {
				echo '<div class="notice is-dismissible error">必须填写URL</div>';
			}
		}
		//刷新所有
		if('刷新所有' == $_POST['action']) {
			$baiduSu = new baiduSu;
			$response = $baiduSu->refresh_cache('', 'all');
			if($response !== true) {
				echo '<div class="notice is-dismissible error">' . $response . '</div>';
			} else {
				echo '<div class="notice is-dismissible updated">刷新CDN缓存成功！</div>';
			}
		}
		
	}
	$options = get_option('baidusu');
	$options = unserialize($options);

?>
<style type="text/css">
	#wpwrap .notice {
		margin-left: 0;
		margin-top: 15px;
		padding: 1em;
	}
	.mywrap {
		background-color: #fff;
		padding: 5px 20px;
		border-radius: 4px;
		border: 1px solid #ccc;
		margin: 15px 15px 15px 0;
	}
	#refresh {
		background-color: #f9f9f9;
		padding: 10px 15px;
		margin: 30px 0 10px 0;
		border-radius: 4px;
		border: 1px solid #ddd;
	}
	#refresh .button {
		margin-top: 10px;
	}
	@media screen and (max-width: 782px) {
		.mywrap .button-primary {
			min-height: 40px;
		}
	}
</style>
<div class="mywrap">
	<h2>百度云加速</h2>
	<p>更新文章自动刷新CDN缓存，新增文章自动刷新首页、文章分类页、标签页缓存。</p>
	<form method="post" action="<?php echo admin_url('options-general.php?page=baidusu'); ?>">
		<table class="form-table">
			<tbody>
				<tr>
					<th scope="row">
						<label for="accesskey">Access Key</label>
					</th>
					<td>
						<input id="accesskey" name="access_key" class="regular-text code" type="text" value="<?php echo isset($options['access_key']) ? $options['access_key'] : ''; ?>" required>
						<p class="description">密钥<a href="https://su.baidu.com/console/index.html#/accountconfig" target="_blank" rel="noopener noreferrer nofollow">登录百度云加速</a>获取</p>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<label for="secretkey">Secret Key</label>
					</th>
					<td>
						<input id="secretkey" name="secret_key" class="regular-text code" type="text" value="<?php echo isset($options['secret_key']) ? $options['secret_key'] : ''; ?>" required>
					</td>
				</tr>
			</tbody>
		</table>
		<p><input class="button-primary" type="submit" value="保存设置">
			<input type="hidden" name="action" value="save"></p>
	</form>
	<form id="refresh" method="post" action="<?php echo admin_url('options-general.php?page=baidusu'); ?>">
		<p id="remind">输入要刷新的URL，每行一条，最多500条</p>
		<textarea id="urls" name="refresh" class="large-text code" rows="5"></textarea>
		<p>
			<input class="button-primary" type="submit" name="action" value="立即刷新">
			<input class="button" type="submit" name="action" value="刷新所有">
		</p>
	</form>
	<script type="text/javascript">
		jQuery(document).ready(function() {
			jQuery("#urls").bind("input propertychange", function() {
				var urls = jQuery(this).val();
				urls = urls.split("\n");
				var urlcount = urls.length;
				var num = 500-urlcount;
				if(num > 0) {
					var free = '还可以输入<span style="color: red">' + num + '</span>个';
				} else {
					var free = '已超出<span style="color: red">' + Math.abs(num) + '</span>个';
				}
				jQuery("#remind").html('已输入<span style="color: red">' + urlcount + '</span>个，' + free);
			});
		});
	</script>
</div>

<?php
}

//添加菜单
function baidusu_menu() {
	add_options_page('百度云加速', '百度云加速', 'administrator', 'baidusu', 'baidusu_control');
}
add_action('admin_menu', 'baidusu_menu');
//插件页面设置链接
add_filter('plugin_action_links', function($links, $file) {
	if($file == plugin_basename(__FILE__)) {
		array_unshift($links, '<a href="options-general.php?page=baidusu">设置</a>');
	}
	return $links;
}, 10, 2);
?>
